package net.qhzw.vip.qiyu.listener;

import net.qhzw.vip.qiyu.entity.*;
import net.qhzw.vip.qiyu.evnet.ReceiptOrderEvent;
import net.qhzw.vip.qiyu.feign.ShopFeignClient;
import net.qhzw.vip.qiyu.mapper.SysUserMapper;
import net.qhzw.vip.qiyu.order.ShopCartEventOrder;
import net.qhzw.vip.qiyu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Component("defaultReceiptOrderListener")
public class ReceiptOrderListener {


    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private ShopAccountService shopAccountService;

    @Autowired
    private DeliveryManService deliveryManService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ShopFeignClient shopFeignClient;

    /***
     * 确认收货
     * 商品销量增加
     */
    @EventListener(ReceiptOrderEvent.class)
    @org.springframework.core.annotation.Order(ShopCartEventOrder.DEFAULT)
    public void defaultReceiptOrderEvent(ReceiptOrderEvent event) {
        Order order = event.getOrder();
        String orderNumber = order.getOrderNumber();
        List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
        for (OrderItem orderItem : orderItems) {
            Long prodId = orderItem.getProdId();
            Product product = productService.getById(prodId);
            if (product == null) {
                return;
            }
            product.setSoldNum(product.getSoldNum() + orderItem.getProdCount());
            productService.updateById(product);
        }
        //运费
//        Double freightAmount = order.getFreightAmount();
//        shopAccount(orderNumber, freightAmount);
//        pointAccount(order, freightAmount);
        //1.车体费用 2.运费 3.平台服务费 4 提货点费用
        //平台使用费
        newBill(order);

    }

    /**
     * 运费
     *
     * @param order
     */
    private void freight(Order order) {
        Long shopId = order.getDvyId();
        Double deliveryPrice = order.getDeliveryPrice();
        if (deliveryPrice > 0) {
            ShopAccount shopAccount = new ShopAccount();
            shopAccount.setShopId(shopId);
            Shop data = shopFeignClient.findById(shopId).getData();
            if (data != null && !StringUtils.isEmpty(data.getShopName())) {
                shopAccount.setShopName(data.getShopName());
            }
            shopAccount.setTransactionType(1);
            shopAccount.setExpenditureAmount(deliveryPrice);
            shopAccount.setRemark(order.getProdName() + "运费");
            shopAccountService.save(shopAccount);
            income(shopId, deliveryPrice);
        }

    }

    /**
     * 平台使用费
     */
    @Transactional
    public void newBill(Order order) {
        String orderNumber = order.getOrderNumber();
        //平台使用费
        //站点服务费
        //运费
        //商户买车费用
        List<OrderItem> orderItemsByOrderNumber = orderItemService.getOrderItemsByOrderNumber(orderNumber);
        for (OrderItem orderItem : orderItemsByOrderNumber) {
            //平台使用费
            platformUsageFee(orderItem);
            //商户销售费用
            merchSalesExpenses(orderItem);
        }

        //站点服务费
        deliveryPntFees(order);
        //运费
        freight(order);
    }

    private void merchSalesExpenses(OrderItem orderItem) {
        Long shopId = orderItem.getShopId();
        Double price = orderItem.getPrice();
        if (orderItem.getMallVip() == 1) {
            price = orderItem.getVipPrice();
        }
        if (price > 0) {
            ShopAccount shopAccount = new ShopAccount();
            shopAccount.setShopId(shopId);
            Shop data = shopFeignClient.findById(shopId).getData();
            if (data != null && !StringUtils.isEmpty(data.getShopName())) {
                shopAccount.setShopName(data.getShopName());
            }
            shopAccount.setTransactionType(1);
            shopAccount.setExpenditureAmount(price);
            shopAccount.setRemark( orderItem.getProdName() + "商品销售收入");
            shopAccountService.save(shopAccount);
            income(shopId, price);
        }


    }

    /**
     * 提货点费用 deliveryPntFees
     *
     * @param order
     */
    private void deliveryPntFees(Order order) {
        Long deliveryId = order.getDeliveryId();
        DeliveryMan deliveryMan = deliveryManService.getById(deliveryId);
        if (deliveryMan != null && deliveryMan.getShopId() != null) {
            String orderNumber = order.getOrderNumber();
            List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
            for (OrderItem orderItem : orderItems) {
                if (orderItem.getMallVip() == 1) {
                    return;
                }
                Double siteFees = orderItem.getSiteFees();
                if (siteFees > 0) {

                    ShopAccount shopAccount = new ShopAccount();
                    shopAccount.setShopId(deliveryMan.getShopId());
                    Shop data = shopFeignClient.findById(deliveryMan.getShopId()).getData();
                    if (data != null && !StringUtils.isEmpty(data.getShopName())) {
                        shopAccount.setShopName(data.getShopName());
                    }
                    shopAccount.setTransactionType(1);
                    shopAccount.setExpenditureAmount(siteFees);
                    shopAccount.setRemark(order.getProdName() + "卖出,站点服务费");
                    shopAccountService.save(shopAccount);
                    income(deliveryMan.getShopId(), siteFees);
                }
            }
        }


    }


    private String nowDate() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.format(now);
    }

    /**
     * 平台使用费
     *
     * @param orderItem
     */
    private void platformUsageFee(OrderItem orderItem) {
        Double price = orderItem.getPlatformServFee();
        if (price > 0) {
            Long prodId = orderItem.getProdId();
            Product product = productService.getById(prodId);
            if (product != null && product.getRevId() != null) {
                SysUser user = sysUserMapper.selectById(product.getRevId());
                if (user != null && user.getShopId() != null) {
                    Long shopId = user.getShopId();
                    ShopAccount shopAccount = new ShopAccount();
                    shopAccount.setShopId(shopId);
                    Shop data = shopFeignClient.findById(shopId).getData();
                    if (data != null && !StringUtils.isEmpty(data.getShopName())) {
                        shopAccount.setShopName(data.getShopName());
                    }
                    shopAccount.setExpenditureAmount(price);
                    shopAccount.setRemark(orderItem.getProdName() + "卖出");
                    shopAccount.setTransactionType(1);
                    shopAccountService.save(shopAccount);
                    income(shopId, price);
                }
            }
        }

    }




    private void income(Long shopId, Double price) {
        informationService.income(shopId, price);
    }



}
